package com.manage.system.web;

import com.manage.common.core.enums.CommonEnum;
import com.manage.common.core.utils.DefaultProfileUtil;
import com.manage.system.svc.properties.ApplicationProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collection;

/**
 * @author zq
 */
@EnableFeignClients(value = "com.manage.*.api")
@EnableDiscoveryClient
@ComponentScan(value = {"com.manage.*"})
@SpringBootApplication
@MapperScan(value = {"com.manage.system.*.mapper"})
@EnableConfigurationProperties({ApplicationProperties.class})
public class ManageSystemApplication {

	private static final Logger log = LoggerFactory.getLogger(ManageSystemApplication.class);

	private final Environment env;

	public ManageSystemApplication(Environment env) {
		this.env = env;
	}

	@PostConstruct
	public void initApplication() {
		Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
		if (activeProfiles.size() > 1) {
			log.error("您的应用程序配置错误!它不应该同时运行{}配置文件。", activeProfiles);
			CommonEnum.ACTIVE_PROFILE_TO_MANY.assertFail("您的应用程序配置错误!它不应该同时运行{0}配置文件。", activeProfiles.toString());
		}
	}

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(ManageSystemApplication.class);
		DefaultProfileUtil.addDefaultProfile(app);
		Environment env = app.run(args).getEnvironment();
		DefaultProfileUtil.logApplicationStartup(env);
	}

}
